function getMaxSortedDistance(array) {
  let max = array[0]
  let min = array[0]
  for (let i=1; i< array.length; i++) {
    if (array[i] > max) {
      max = array[i]
    }
    if (array[i] < min) {
      min = array[i]
    }
  }
  let d = max - min;
  if (d == 0) {
    return 0;
  }

  let bucketNum = array.length;
  let buckets = new Array(bucketNum);
  for (let i=0; i< bucketNum; i++) {
    buckets[i] = new Bucket();
  }

  for (let i=0; i< array.length; i++) {
    let index = Math.floor(((array[i] - min) * (bucketNum - 1)) / d);
    // console.log({index});
    // console.log(buckets[index]);
    
    
    if (buckets[index].min == null || buckets[index].min >array[i]) {
      buckets[index].min = array[i]
    }
    if (buckets[index].max == null || buckets[index].max < array[i]) {
      buckets[index].max = array[i];
    }
  }

  let leftMax = buckets[0].max;
  let maxDistance = 0;
  for (let i=1; i< buckets.length; i++) {
    if (buckets[i].min == null) {
      continue;
    }
    if (buckets[i].min - leftMax > maxDistance) {
      maxDistance = buckets[i].min - leftMax;
    }
    leftMax = buckets[i].max;
  }
  return maxDistance;
}

class Bucket {
  constructor(min, max) {
    this.min = min;
    this.max = max;
  }
}

let array = [2,6,3,4,5,10,9]

console.log(getMaxSortedDistance(array));
